﻿@using Rj.PersonnelVerification.Client.Models
@using Rj.PersonnelVerification.Shared.Models.Request
@using Rj.PersonnelVerification.Shared.Models.Response
<MudDialog Options="_dialogOptions">
    <TitleContent>
        <MudText Typo="Typo.h6">
            <MudIcon Icon="@Icons.Material.Filled.Edit" Class="mr-8" />上传中
        </MudText>
    </TitleContent>
    <DialogContent>
        <MudSimpleTable Style="overflow-x: auto; height:300px;" Hover="true" Dense="true" FixedHeader="true">
            <thead>
                <tr>
                    <th>住院号</th>
                    <th>姓名</th>
                    <th>交易编码</th>
                    <th>状态</th>
                    <th>消息</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var row in verifiRecords)
                {
                    <tr>
                        <td>@row.HostNo</td>
                        <td>@row.PersonName</td>
                        <td>@row.Infono</td>
                        <td>@row.Status</td>
                        <td>@row.Msg</td>
                    </tr>
                }
            </tbody>
        </MudSimpleTable>
        <MudProgressLinear Color="Color.Info" Size="Size.Medium" Value="_progressValue" Class="my-7" />
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="Submit" Class="px-10" @bind-Disabled="closeDisable">关闭</MudButton>
    </DialogActions>
</MudDialog>
@code {
    [CascadingParameter]
    private MudDialogInstance MudDialog { get; set; }
    [Parameter]
    public List<QueryNoUploadResponse> SelectedPploadResponses { get; set; } = new List<QueryNoUploadResponse>();
    [Inject]
    public IHttpClientFactory httpClientFactory { get; set; }
    private bool closeDisable;

    private readonly DialogOptions _dialogOptions = new() { FullWidth = true, DisableBackdropClick = true };

    private List<VerifiRecord> verifiRecords = new List<VerifiRecord>();
    public int _progressValue { get; set; }

    private void Submit() => MudDialog.Close(DialogResult.Ok(true));

    private void Cancel() => MudDialog.Cancel();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        closeDisable = true;
        verifiRecords.Clear();
        foreach (var noUploadResponse in SelectedPploadResponses)
        {
            foreach (var item in noUploadResponse.NoUploadInfnos)
            {
                VerifiRecord record = new VerifiRecord();
                record.HostNo = noUploadResponse.HospNo;
                record.PersonName = noUploadResponse.PatName;
                record.Infono = item;
                record.Status = "等待中";
                verifiRecords.Add(record);
            }
        }
        _progressValue = 0;
        var queryNoUploadResponses = SelectedPploadResponses.ToList();
        for (int i = 0; i < queryNoUploadResponses.Count; i++)
        {
            var noUploadResponse = queryNoUploadResponses[i];
            _progressValue = ((i * 100) / (SelectedPploadResponses.Count));
            StateHasChanged();
            foreach (var item in noUploadResponse.NoUploadInfnos)
            {
                var record = verifiRecords.Find(f => f.Infono == item && f.HostNo == noUploadResponse.HospNo);
                record.Status = "上传中";
                VerificationUploadRequest request = new VerificationUploadRequest()
                    {
                        UploadRecords = new List<Rj.PersonnelVerification.Shared.Models.Request.UploadRecord>()
                        {
                            new PersonnelVerification.Shared.Models.Request.UploadRecord()
                            {
                                RegNo=noUploadResponse.RegNo,
                                NoUploadInfnos=new List<string>() { item },
                            }
                        },
                    };
                if (noUploadResponse.OutTime == null && item == "2402")
                {
                    record.Msg = "患者未出院，跳过上传";
                    record.Status = "失败";
                    continue;
                }
                var httpClient = httpClientFactory.CreateClient("ApiClient");
                var response = await httpClient.PostAsJsonAsync("api/Verification/Upload", request);
                if (!response.IsSuccessStatusCode)
                {
                    record.Msg = await response.Content.ReadAsStringAsync();
                    record.Status = "失败";
                    continue;
                }
                VerificationUploadResponse uploadResponse = await response.Content.ReadFromJsonAsync<VerificationUploadResponse>();
                if (uploadResponse.UploadRecords.FirstOrDefault().Success)
                    record.Status = "成功";
                else
                {
                    record.Status = "失败";
                    record.Msg = uploadResponse.UploadRecords.FirstOrDefault().Error;
                }
            }
        }
        closeDisable = false;
    }
}